package com.shine.controller;

import cn.hutool.core.bean.BeanUtil;
import com.shine.info.MenuInfo;
import com.shine.info.UserInfo;
import com.shine.request.CaptchaVerifyRequest;
import com.shine.request.LoginRequest;
import com.shine.request.UserRegisterRequest;
import com.shine.response.CaptchaResponse;
import com.shine.response.MenuResponse;
import com.shine.response.UserDetailResponse;
import com.shine.response.UserLoginResponse;
import com.shine.security.UserPrincipal;
import com.shine.service.LoginService;
import com.shine.service.MenuService;
import com.shine.utils.Result;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author huihui
 * @date 2025/10/11 15:57
 * @description LoginController
 */
@Slf4j
@Tag(name = "用户登录管理 Controller", description = "用户登录管理 Controller")
@RestController
@RequestMapping("/system")
public class LoginController extends BaseController {

    @Resource
    private LoginService loginService;

    @Resource
    private MenuService menuService;

    @Operation(summary = "获取验证码图片")
    @GetMapping("/captcha/getCaptcha")
    public Result<CaptchaResponse> getCaptcha() {
        CaptchaResponse response = loginService.getCaptcha();
        return success(response);
    }

    @Operation(summary = "验证验证码")
    @PostMapping("/captcha/verify")
    public Result<?> verifyCaptcha(@RequestBody CaptchaVerifyRequest request) {
        loginService.verifyCaptcha(request);
        return success();
    }

    @Operation(summary = "登录")
    @PostMapping("/user/login")
    public Result<UserLoginResponse> login(@RequestBody LoginRequest request) {
        UserLoginResponse response = loginService.login(request);
        return success(response);
    }

    @Operation(summary = "获取用户信息")
    @GetMapping("/user/getUserInfo")
    public Result<UserDetailResponse> getUserInfo() {
        UserDetailResponse response = new UserDetailResponse();
        UserPrincipal userInfo = loginService.getUserInfo();
        userInfo.setPassword("你想知道个屁");
        BeanUtil.copyProperties(userInfo, response, true);
        // 菜单
        List<MenuResponse> menulist = menuService.listByUserId(userInfo.getId());
        response.setMenuList(menulist);
        return success(response);
    }

    @Operation(summary = "用户注册")
    @PostMapping("/user/register")
    public Result<Boolean> registerUser(@RequestBody UserRegisterRequest request) {
        return Result.success(loginService.register(request));
    }

}
